package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Permission;
import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.domain.SystemMenu;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.PermissionService;
import cn.wolfcode.wms.service.RoleService;
import cn.wolfcode.wms.service.SystemMenuService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private SystemMenuService systemMenuService;
    @RequiredPermission("角色查询")
    @RequestMapping("/list")
    public String list(Map map, QueryObject qo) {
        PageResult result = roleService.query(qo);
        System.out.println(qo.getPageSize());
        System.out.println("=========================================");
        map.put("result",result);
        return "role/list";
    }
    @RequiredPermission("角色编辑")
    @RequestMapping("/input")
    public String input(Map map, Long id) {
        List<SystemMenu> menus = systemMenuService.listAll();
        map.put("menus",menus);
        List<Permission> list = permissionService.selectAll();
        map.put("list",list);
        if (id != null && id != 0) {
            Role dapt = roleService.getById(id);
            map.put("dapt", dapt);
        }
        return "/role/input";
    }
    @RequiredPermission("角色删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            roleService.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return  JSONResult.success("删除成功");
    }
    @RequiredPermission("部门新增/修改")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Role dapt,Long[] ids,Long[] menuIds) {
        //
        try {
            roleService.updateOrSave(dapt,ids,menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return JSONResult.success("操作成功");
    }
}
